package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.EquipmentCycleTimeDao;
import cn.b.sky.mes.em.model.Equipment;
import cn.b.sky.mes.em.model.EquipmentCycleTime;
import cn.b.sky.mes.em.model.condition.EquipmentCondition;
import cn.b.sky.mes.em.model.condition.EquipmentCycleTimeCondition;
import cn.b.sky.mes.em.service.EquipmentCycleTimeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("equipmentCycleTimeService")
public class EquipmentCycleTimeServiceImpl implements EquipmentCycleTimeService {

  private static final Logger LOG = LoggerFactory.getLogger(EquipmentCycleTimeServiceImpl.class);

  @Resource
  EquipmentCycleTimeDao timeDao;

  @Override
  public List<EquipmentCycleTime> selectEquipmentCycleTimeListByCondition(EquipmentCycleTimeCondition condition) {
    if (condition == null) {
      condition = new EquipmentCycleTimeCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return timeDao.selectEquipmentCycleTimeListByCondition(condition);
  }

  @Override
  public int selectEquipmentCycleTimeCountByCondition(EquipmentCycleTimeCondition condition) {
    if (condition == null) {
      condition = new EquipmentCycleTimeCondition();
    }
    return timeDao.selectEquipmentCycleTimeCountByCondition(condition);
  }

  @Override
  public EquipmentCycleTime getEquipmentCycleTime(String equipmentCode, String siteCode) {
    if (StringUtils.isBlank(equipmentCode) || StringUtils.isBlank(siteCode)) {
      return null;
    } else {
      return timeDao.getEquipmentCycleTime(equipmentCode, siteCode);
    }
  }

  @Override
  public void insertEquipmentCycleTime(EquipmentCycleTime time) {
    if (StringUtils.isBlank(time.getEquipmentCode())) {
      return;
    }
    timeDao.insertEquipmentCycleTime(time);

  }

  @Override
  public void updateEquipmentCycleTime(EquipmentCycleTime time) {
    if (StringUtils.isBlank(time.getEquipmentCode())) {
      return;
    }
    timeDao.updateEquipmentCycleTime(time);

  }

  @Override
  public BatchJobReport deleteEquipmentCycleTime(String equipmentCode, String siteCode) {
    if (!StringUtils.isBlank(equipmentCode)) {
      String message = equipmentCode;
      String cause = "";
      boolean success = true;
      try {
        timeDao.deleteEquipmentCycleTime(equipmentCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          // 外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "equipmentCode is null", false);
    }

  }

  @Override
  public List<Equipment> selectAllEquipmentList(EquipmentCondition condition) {
    return timeDao.selectAllEquipmentList(condition);
  }

  @Override
  public List<Equipment> selectAllEquipmentListForSuggest(EquipmentCondition condition) {
    return timeDao.selectAllEquipmentListForSuggest(condition);
  }

}
